package bloodfang.gui;

import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JPanel;

import bloodfang.gui.GUI2.ShowGemListListener;
import bloodfang.gui.GUI2.UpgradeLevelListener;

public class SlotView extends JPanel implements PropertyChangeListener {
	
	DBCReader dbcReader = DBCReader.getInstance();
	
	private static final long serialVersionUID = 3999907598217391220L;
	private SlotViewButton icon = new SlotViewButton();
	private SlotViewButton[] gems = new SlotViewButton[3];
	private SlotViewButton enchant = new SlotViewButton();
	private SlotViewButton upgrade_level = new SlotViewButton(); // has methods such as setBorderPainted() and setContentAreaFilled()
	private SlotViewButton random_suffix = new SlotViewButton();
	
	public SlotView(){
		gems[0] = new SlotViewButton();
		gems[1] = new SlotViewButton();
		gems[2] = new SlotViewButton();
		this.setLayout(null);
		//this.setBackground(Color.PINK);
		this.setSize(20, 58);
		//this.setBorder(BorderFactory.createLineBorder(Color.black));
		//this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		//this.setAlignmentX(LEFT_ALIGNMENT);
		
		
		icon.setBounds(0, 0, 56, 56);
		this.add(icon);
		upgrade_level.setBounds(57, 0, 16, 16);
		this.add(upgrade_level);
		random_suffix.setBounds(78, 0, 100, 16);
		this.add(random_suffix);
		gems[0].setBounds(57, 17, 16, 16);
		this.add(gems[0]);
		gems[1].setBounds(78, 17, 16, 16);
		this.add(gems[1]);
		gems[2].setBounds(99, 17, 16, 16);
		this.add(gems[2]);
		enchant.setBounds(128, 34, 70, 20);
		this.add(enchant);
	}
	
	public void addUpgradeLevelListener(UpgradeLevelListener l){
		upgrade_level.addActionListener(l);
	}
	
	public void setAll(EquippedItem i){
		
		// item
		setItemId(i.item_id);
		
		// gems
		for(int j = 0; j < 3; j++){
			setGemId(j, i.gem_ids[j]);
		}
		//this.enchant.setText(""+dbcReader.getEnchantName(i.getEnchant()));
		System.out.println("#1 id:    " + i.getEnchant());
		System.out.println("#2 name:  " + dbcReader.getEnchantName(i.getEnchant()));
		System.out.println("#3 value: " + dbcReader.getEnchantValue(i.getEnchant(),0));
		this.enchant.setText(String.valueOf(i.getEnchant()));
		this.upgrade_level.setText(""+i.getUpgradeLevel());
		String suffixname = dbcReader.getSuffixName(i.getRandomSuffix());
		if(!suffixname.equals("0"))
			this.random_suffix.setText(String.valueOf(dbcReader.getSuffixName(i.getRandomSuffix())));
		else
			this.random_suffix.setVisible(false);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equals("itemId")){
			setItemId((Integer) evt.getNewValue());
		}else if(evt.getPropertyName().equals("gem0")){
			setGemId(0, (Integer) evt.getNewValue());
		}else if(evt.getPropertyName().equals("gem1")){
			setGemId(1, (Integer) evt.getNewValue());
		}else if(evt.getPropertyName().equals("gem2")){
			setGemId(2, (Integer) evt.getNewValue());
		}else if(evt.getPropertyName().equals("upgradeLevel")){
			this.upgrade_level.setText(Integer.toString((int) evt.getNewValue()));
		}else if(evt.getPropertyName().equals("randomSuffix"))
			this.random_suffix.setText(""+dbcReader.getSuffixName((int) evt.getNewValue()));
	}
	
	private void setItemId(int itemId) {
		Item item = ItemDB.get(itemId, 0, 0);
		if(item != null){
			ChangeIconTask task = new ChangeIconTask(this.icon,item.getIcon());
			task.execute();
		}
	}
	
	private void setGemId(int socketIndex, int itemId) {
		Gem gem = ItemDB.getGem(itemId);
		if(gem != null){
			ChangeIconTask task = new ChangeIconTask(this.gems[socketIndex],gem.getIcon(),true);
			task.execute();
		}
	}

	public void addItemListener(ActionListener showItemListListener) {
		this.icon.addActionListener(showItemListListener);		
	}
	
	public void addGemListener(ShowGemListListener showGemListListener) {
		this.gems[showGemListListener.getSocketIndex()].addActionListener(showGemListListener);
	}
}
